package com.three.box.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.three.box.R;
import com.three.box.entity.ProductConfig;
import com.three.box.entity.ProductModel;
import com.three.box.helper.LogUtils;

import java.util.List;

public class ProductConfigAdapter extends BaseExpandableListAdapter {
    private static final String TAG = "ProductConfigAdapter";

    private final List<ProductConfig> data;

    private final LayoutInflater inflater;

    public ProductConfigAdapter(Context context, List<ProductConfig> data) {
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.get(groupPosition).getProductModels().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).getProductModels().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder parentViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_parent_add_product, parent, false);
            parentViewHolder = new ParentViewHolder();
            parentViewHolder.productCb = convertView.findViewById(R.id.product_cb);
            parentViewHolder.nameTv = convertView.findViewById(R.id.name_tv);
            parentViewHolder.indicatorIv = convertView.findViewById(R.id.indicator_iv);
            convertView.setTag(parentViewHolder);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }
        ProductConfig item = data.get(groupPosition);
        parentViewHolder.nameTv.setText(item.getDesc());
        parentViewHolder.indicatorIv.setSelected(isExpanded);
        parentViewHolder.productCb.setOnClickListener(v -> {
            boolean checked = item.getChecked();
            item.setChecked(!checked);
            item.getProductModels().forEach(productModel -> productModel.setChecked(!checked));
            notifyDataSetChanged();
        });
        parentViewHolder.productCb.setChecked(item.getChecked());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_child_add_product, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.subProductCb = convertView.findViewById(R.id.sub_product_cb);
            childViewHolder.modelTv = convertView.findViewById(R.id.model_tv);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        ProductConfig item = data.get(groupPosition);
        ProductModel subItem = item.getProductModels().get(childPosition);
        childViewHolder.modelTv.setText(subItem.getName());
        childViewHolder.subProductCb.setOnClickListener(v -> {
            boolean checked = subItem.getChecked();
            LogUtils.INSTANCE.d(TAG, "is checked " + checked);
            subItem.setChecked(!checked);
            LogUtils.INSTANCE.d(TAG, "item.checkAll() " + item.checkAll());
            item.setChecked(item.checkAll());
            notifyDataSetChanged();
        });
        childViewHolder.subProductCb.setChecked(subItem.getChecked());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public static class ParentViewHolder {
        CheckBox productCb;
        TextView nameTv;
        ImageView indicatorIv;
    }

    public static class ChildViewHolder {
        CheckBox subProductCb;
        TextView modelTv;
    }
}
